package com.wistech.search.solr.extend;

import java.util.Map;

import com.wistech.utils.ConverterUtils;

/**
 * 外部数据处理器基类
 * 
 * @author Liuye
 * 
 * @param <T>
 */
public abstract class AbstractExternalDataHandler<T> implements DocumentHander {
	static {
		ConverterUtils.registConverter();
	}

	@Override
	public boolean add(SolrIndexContext context) {
		//删除value为null的键值对
		Map<String, Object> props = context.getModel();
		for (Map.Entry<String, Object> entry : props.entrySet()) {
			if (entry.getValue() == null) {
				props.remove(entry.getKey());
			}
		}
		saveData(toModel(props));
		return true;
	}

	@Override
	public boolean delete(String id) {
		deleteData(id);
		return true;
	}

	protected abstract T toModel(Map<String, Object> props);

	protected abstract void saveData(T t);

	protected abstract void deleteData(String id);

}
